using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;

public class AndroidUtils
{
    private static AndroidJavaObject vibrationPlugin;

    // DllImport 语句用于调用iOS插件中的C函数
    [DllImport("__Internal")]
    private static extern void setVibrator(float intensity);

    // Start is called before the first frame update
    public static void Init()
    {
        if(Application.platform == RuntimePlatform.Android) {
            using(var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) {
                var activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
                if(activity != null) {
                    Debug.Log("has activity");
                }
                var context = activity.Call<AndroidJavaObject>("getApplicationContext");
                if(context != null) {
                    Debug.Log("has context");
                }
                vibrationPlugin = new AndroidJavaObject("com.lck.unityviabration.VibrationPlugin",context);
            }
        }
    }

    public static void Vibrate(long milliseconds) {
        if(vibrationPlugin == null) {
            Debug.Log("vibrationPlugin is null");
            
        }
        if(Application.platform == RuntimePlatform.Android && vibrationPlugin != null) {
            vibrationPlugin.Call("vibrate",milliseconds);
        } else if(Application.platform == RuntimePlatform.IPhonePlayer) {
            Debug.Log("调用震动功能");
            setVibrator(1.5f);
        }
    }

    public static void Cancel() {
        if(Application.platform == RuntimePlatform.Android && vibrationPlugin != null) {
            vibrationPlugin.Call("cancel");
        }
    }
}
